#include "gcase.h"

#include "mainwindow.h"
#include "plateau.h"

#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtGui/QColor>
#include <QtGui/QGraphicsSceneMouseEvent>
#include <QtGui/QGraphicsSceneDragDropEvent>

GCase::GCase(int pX, int pY, int pNum, GPlateau *parent) : QGraphicsEllipseItem(pX, pY, 90, 90), m_X(pX), m_Y(pY), m_num(pNum), m_parent(parent) {
    //setBrush(QBrush(Qt::blue));
    setZValue(1);

    m_nbGraines = 4;
	m_brushAct = QBrush(Qt::NoBrush);

	setAcceptHoverEvents(true);

	m_timer = new QTimer();
	QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(resetColor()));
}

void GCase::mousePressEvent(QGraphicsSceneMouseEvent *event) {
	Q_UNUSED(event);
	m_parent->onCaseClicked(this);
}

void GCase::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
	Q_UNUSED(event);
	if (m_nbGraines > 0 &&
			((m_num >= 0 && m_num <= 5 && ((MainWindow*)(m_parent->parent()))->isBottomClickable()) ||
			 (m_num >= 6 && m_num <= 11 && ((MainWindow*)(m_parent->parent()))->isTopClickable())))
		setBrush(QBrush(Qt::blue, Qt::Dense4Pattern));
	else
		setBrush(QBrush(Qt::gray, Qt::Dense4Pattern));
}

void GCase::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
	Q_UNUSED(event);
	setBrush(m_brushAct);
}

void GCase::resetColor() {
	m_brushAct = QBrush(Qt::NoBrush);
	setBrush(m_brushAct);
	m_timer->stop();
}

void GCase::setNbGraines(int pNbGraines) {
	m_nbGraines = pNbGraines;
	m_brushAct = QBrush((((MainWindow*)(m_parent->parent()))->joueurActuel() == 1 ? Qt::red : Qt::green), Qt::Dense4Pattern);
	setBrush(m_brushAct);
	m_timer->start(1000);
}
